package com.dd.shop4j.console.goods.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dd.shop4j.console.goods.service.GoodsCategManagerService;
import com.dd.shop4j.goods.model.GoodsCateg;

/**
 * 商品分类Controller
 * @author tangshu
 *
 */
@Controller
@RequestMapping("/admin/categ")
public class GoodsCategController {
	
	@Resource
	private GoodsCategManagerService goodsCategManagerService;

	@Deprecated
	@RequestMapping("/list")
	public ModelAndView list() {
		ModelAndView view = new ModelAndView("/admin/goods/categ_list");
		view.addObject("categs", goodsCategManagerService.getAllGoodsCateg());
		return view;
	}
	
	@RequestMapping
	public String init() {
		return "/admin/goods/categ_list";
	}
	
	@RequestMapping(value = "/insert", method = RequestMethod.GET)
	public ModelAndView insert() {
		ModelAndView view = new ModelAndView("/admin/goods/categ_form");
		view.addObject("categ", new GoodsCateg());
		return view;
	}
	
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public String insert(@ModelAttribute("categ") GoodsCateg categ) {
		goodsCategManagerService.insertGoodsCateg(categ);
		return "/admin/goods/categ_list";
	}
	
	@ResponseBody
	@RequestMapping("/gridTree")
	public String gridTree() {
		return goodsCategManagerService.getGoodsCategTree();
	}
	
	@RequestMapping("/delete/{categId}")
	public String delete(@PathVariable("categId")Integer categId) {
		goodsCategManagerService.deleteGoodsCateg(categId);
		return "redirect:/admin/categ/list";
	}

	@RequestMapping(value = "/update/{categId}", method = RequestMethod.GET)
	public ModelAndView update(@PathVariable("categId") Integer categId) {
		ModelAndView view = new ModelAndView("/admin/goods/categ_form");
		view.addObject("categ", goodsCategManagerService.loadGoodsCateg(categId));
		return view;
	}
	
	@RequestMapping(value = "/update/{categId}", method = RequestMethod.POST)
	public String update(@PathVariable("categId") Integer categId,
			@ModelAttribute("categ") GoodsCateg categ) {
		categ.setId(categId);
		goodsCategManagerService.updateGoodsCateg(categ);
		return "/admin/goods/categ_form";
	}
}
